package org.teachertools.web.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.teachertools.data.Note;
import org.teachertools.services.NoteService;
import org.teachertools.web.forms.NoteForm;
import org.teachertools.web.forms.NoteFormMapping;

public class NoteViewAction extends CommonAction {

    @Override
    protected ActionForward process(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        NoteService noteService = this.getServiceLocator().getNoteService();
        Long id = Long.parseLong(request.getParameter("id"));
        Note note = noteService.get(id);
        NoteFormMapping.mappingDTOToForm(note, (NoteForm) form, request);
        ((NoteForm) form).setIsUpdate(Boolean.TRUE);

        Long idCategorie = note.getCompetence().getMatiere().getCategorie().getId();
        request.setAttribute("idCategorie", idCategorie);
        Long idMatiere = note.getCompetence().getMatiere().getId();
        request.setAttribute("idMatiere", idMatiere);
        String idClasse = note.getEleve().getClasse();
        request.setAttribute("idClasse", idClasse);

        addCategoryList(request);
        addMatiereList(request);
        addCompetenceList(request);
        addClassList(request);
        addEleveList(request);

        return (mapping.findForward(CONTINUE));
    }
}
